package zhu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// Course类，用于表示课程信息
class Course {
    private String CourseId;
    private String CourseName;
    private String CourseDescription;

    // 构造器
    public Course(String CourseId, String CourseName, String CourseDescription) {
        this.CourseId = CourseId;
        this.CourseName = CourseName;
        this.CourseDescription = CourseDescription;
    }

    // Getter和Setter方法
    public String getCourseId() {
        return CourseId;
    }

    public void setCourseId(String CourseId) {
        this.CourseId = CourseId;
    }

    public String getCourseName() {
        return CourseName;
    }

    public void setCourseName(String CourseName) {
        this.CourseName = CourseName;
    }

    public String getCourseDescription() {
        return CourseDescription;
    }

    public void setCourseDescription(String CourseDescription) {
        this.CourseDescription = CourseDescription;
    }
}

// CourseManagementDialogs类，用于管理课程信息的对话框
public class CourseManagementDialogs {
    // 连接数据库
    private Connection connectToDatabase() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/exam_management_system", "root", "zhu751111");
    }

    // 添加课程对话框
    public void openAddCourseDialog(JFrame frame) {
        JTextField courseIdField = new JTextField(10);
        JTextField courseNameField = new JTextField(10);
        JTextField courseDescriptionField = new JTextField(10);
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("输入课程编号："));
        panel.add(courseIdField);
        panel.add(new JLabel("输入课程名称："));
        panel.add(courseNameField);
        panel.add(new JLabel("输入课程描述："));
        panel.add(courseDescriptionField);

        int result = JOptionPane.showConfirmDialog(frame, panel,
                "添加课程", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String courseId = courseIdField.getText();
            String courseName = courseNameField.getText();
            String courseDescription = courseDescriptionField.getText();
            try (Connection conn = connectToDatabase();
                 PreparedStatement pstmt = conn.prepareStatement(
                         "INSERT INTO Courses (CourseId, CourseName, CourseDescription) VALUES (?, ?, ?)")) {
                pstmt.setString(1, courseId);
                pstmt.setString(2, courseName);
                pstmt.setString(3, courseDescription);
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(frame, "课程添加成功！");
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(frame, "添加课程失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // 删除课程对话框
    public void openDeleteCourseDialog(JFrame frame) {
        JTextField courseIdField = new JTextField(10);
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("输入要删除的课程编号："));
        panel.add(courseIdField);

        int result = JOptionPane.showConfirmDialog(frame, panel,
                "删除课程", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String courseId = courseIdField.getText();
            try (Connection conn = connectToDatabase();
                 PreparedStatement pstmt = conn.prepareStatement(
                         "DELETE FROM Courses WHERE CourseId = ?")) {
                pstmt.setString(1, courseId);
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(frame, "课程删除成功！");
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(frame, "删除课程失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // 修改课程对话框
    public void openModifyCourseDialog(JFrame frame) {
        JTextField courseIdField = new JTextField(10);
        JTextField courseNameField = new JTextField(10);
        JTextField courseDescriptionField = new JTextField(10);
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("输入要修改的课程编号："));
        panel.add(courseIdField);
        panel.add(new JLabel("输入新的课程名称："));
        panel.add(courseNameField);
        panel.add(new JLabel("输入新的课程描述："));
        panel.add(courseDescriptionField);

        int result = JOptionPane.showConfirmDialog(frame, panel,
                "修改课程", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String courseId = courseIdField.getText();
            String courseName = courseNameField.getText();
            String courseDescription = courseDescriptionField.getText();
            try (Connection conn = connectToDatabase();
                 PreparedStatement pstmt = conn.prepareStatement(
                         "UPDATE Courses SET CourseName = ?, CourseDescription = ? WHERE CourseId = ?")) {
                pstmt.setString(1, courseName);
                pstmt.setString(2, courseDescription);
                pstmt.setString(3, courseId);
                int rowsAffected = pstmt.executeUpdate();
                if (rowsAffected > 0) {
                    JOptionPane.showMessageDialog(frame, "课程修改成功！");
                } else {
                    JOptionPane.showMessageDialog(frame, "没有找到指定的课程编号。", "警告", JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(frame, "修改课程失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
        }

        // 查询课程对话框
        public void openQueryCourseDialog(JFrame frame) {
            JTextField courseIdField = new JTextField(10);
            JTextField courseNameField = new JTextField(10);
            JPanel panel = new JPanel(new GridLayout(0, 1));
            panel.add(new JLabel("输入课程编号（部分）："));
            panel.add(courseIdField);
            panel.add(new JLabel("输入课程名称（部分）："));
            panel.add(courseNameField);

            int result = JOptionPane.showConfirmDialog(frame, panel,
                    "查询课程", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                String courseIdKeyword = courseIdField.getText();
                String courseNameKeyword = courseNameField.getText();
                StringBuilder message = new StringBuilder();

                try (Connection conn = connectToDatabase();
                     PreparedStatement pstmt = conn.prepareStatement(
                             "SELECT * FROM Courses WHERE CourseId LIKE ? AND CourseName LIKE ?")) {
                    pstmt.setString(1, "%" + courseIdKeyword + "%");
                    pstmt.setString(2, "%" + courseNameKeyword + "%");
                    ResultSet rs = pstmt.executeQuery();

                    while (rs.next()) {
                        message.append("课程编号：").append(rs.getString("CourseId"))
                               .append("\n课程名称：").append(rs.getString("CourseName"))
                               .append("\n课程描述：").append(rs.getString("CourseDescription"))
                               .append("\n\n");
                    }
                    if (message.length() == 0) {
                        JOptionPane.showMessageDialog(frame, "没有找到匹配的课程。");
                    } else {
                        JOptionPane.showMessageDialog(frame, new JScrollPane(new JTextArea(message.toString())));
                    }
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(frame, "查询课程失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        
    }


        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new AdminPanel().setVisible(true));
        }
    }

